package com.blog.controller;

import com.blog.Utils.Result;
import com.blog.bean.dto.PageDTO;
import com.blog.service.DiscussPostService;
import com.blog.bean.vo.PageAndUserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author 任浩
 * @version 1.0
 */

@RestController
@RequestMapping("/discussPost")
@Slf4j
public class HomeController {

    @Autowired
    private DiscussPostService discussPostService;

    /**
     * 对首页的帖子进行分页查询.将查询的结果返回给客户端。
     * @return
     */
    @GetMapping
    public Result discussPost(PageDTO page, HttpSession httpSession, @RequestParam("scoreMode") Integer scoreMode,
                                @RequestParam("userIdMode") Integer userIdMode ){
        PageAndUserVO pageAndUserVO = new PageAndUserVO();
        if((int) userIdMode == 0){
            //没有查找自己的帖子。
            //得到当前用户id，去查找当前用户未读的信息。
            Integer unreadUserId = (Integer)httpSession.getAttribute("userId");
            pageAndUserVO = discussPostService.queryPost(page.getPage(),page.getPageNum(),null,scoreMode,unreadUserId);
        }
        else {
            //查找自己的帖子。
            //获得当前用户的id
            Integer userId = (Integer)httpSession.getAttribute("userId");
            pageAndUserVO = discussPostService.queryPost(page.getPage(),page.getPageNum(),userId,scoreMode,userId);
        }

        //打印返回出来的数据
        return Result.success(pageAndUserVO);
    }
}
